// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Machine.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.FiniteStateMachine
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// The Machine is an abstract class expected to be implemented with app-specific methods
    /// representing inputs to the system and setting up the initial states and transitions
    /// of the system. Machine contains the current State and (for convenience) the current
    /// context. A single Update() method is expected to be called with context changes
    /// due to external inputs.
    /// </summary>
    /// <typeparam name="TInput">Inputs to the FSM from the outside world.</typeparam>
    /// <typeparam name="TOutput">Output from the FSM to the outside world.</typeparam>
    /// <typeparam name="TContext">Concrete context type implementing IContext.</typeparam>
    public abstract class Machine<TInput, TOutput, TContext>
        where TContext : IContext<TInput, TOutput>
    {
        /// <summary>
        /// Gets current state.
        /// </summary>
        protected State<TContext> State { get; private set; }

        /// <summary>
        /// Gets or sets current context.
        /// </summary>
        protected TContext Context { get; set; }

        /// <summary>
        /// Initializes Machine with initial context and state.
        /// </summary>
        /// <remarks>Causes transition to initial state; firing onEnter if appropriate.</remarks>
        /// <param name="context">Initial context.</param>
        /// <param name="state">Initial state.</param>
        public void Initialize(TContext context, State<TContext> state)
        {
            this.Context = context;
            this.State = new State<TContext>("Init");
            this.State.AddTransition(_ => true, state);
            this.Update(context.Input);
        }

        /// <summary>
        /// Update the state machine.
        /// </summary>
        /// <param name="input">Input portion of context.</param>
        /// <returns>Output portion of context.</returns>
        protected virtual TOutput Update(TInput input)
        {
            this.Context.Input = input;
            TContext newContext;
            this.State = this.State.Updated(this.Context, out newContext);
            this.Context = newContext;
            return this.Context.Output;
        }
    }
}
